package com.nangle.managehost.fragmet;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;


import com.nangle.managehost.BaseFargment;
import com.nangle.managehost.MainActivity;
import com.nangle.managehost.R;
import com.nangle.managehost.adapter.DeviceGridViewAdapter;
import com.nangle.managehost.adapter.DeviceWardAdapter;
import com.nangle.managehost.dialog.MyDialog;
import com.nangle.managehost.entity.Bed;
import com.nangle.managehost.entity.Room;
import com.nangle.managehost.manager.BedManger;
import com.nangle.managehost.manager.RoomManager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.Date;
import java.util.List;


/**
 * 作者：Administrator on 2016/8/31 13:54
 * 描述：
 */
public class DeviceFragment extends BaseFargment {
    private GridView bed_gridview;
    private GridView ward_gridview;
    private List<Bed> beds = new ArrayList<>();
    private DeviceGridViewAdapter deviceGridViewAdapter;
    private DeviceWardAdapter deviceWardAdapter;
    private Context context;
    private List<Room> rooms = new ArrayList<>();
    private String startTime = "2017-03-19 13:31:40";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getBedCount();
        getRoomCount();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.device_fragment, container, false);
        bed_gridview = (GridView) view.findViewById(R.id.device_bedgrid);
        ward_gridview = (GridView) view.findViewById(R.id.device_wardgrid);
        deviceGridViewAdapter = new DeviceGridViewAdapter(getActivity(), beds);
        deviceWardAdapter = new DeviceWardAdapter(getActivity(),rooms);
        bed_gridview.setAdapter(deviceGridViewAdapter);
        ward_gridview.setAdapter(deviceWardAdapter);
        bed_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==12){
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                    String str = formatter.format(curDate);
                    String time = getTime(str,startTime);
                    showDisDialog(time,"2017-04-03 14:12:43");
                }else{

                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                    String str = formatter.format(curDate);
                    String time = getTime(str,startTime);
                    showFindDialog(time);
                }
            }
        });
        ward_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==5){
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                    String str = formatter.format(curDate);
                    String time = getTime(str,startTime);
                    showDisDialog(time,"2017-04-09 14:53:12");
                }else{
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                    String str = formatter.format(curDate);
                    String time = getTime(str,startTime);
                    showFindDialog(time);
                }
            }
        });
        return view;
    }
    private void getBedCount(){
        beds = BedManger.getInstance(mActivity).getBedList();
    }
    private void getRoomCount(){
        rooms = RoomManager.getInstance(mActivity).getRoomList();
    }

    private void showDisDialog(String timelong,String disLong){
        View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_disappear, null);
        TextView textView1 = (TextView)view.findViewById(R.id.tv_dis_strat_time);
        TextView textView2 = (TextView)view.findViewById(R.id.tv_dis_run_time);
        TextView textView3 = (TextView)view.findViewById(R.id.tv_dis_time);
        TextView textView4 = (TextView)view.findViewById(R.id.tv_dis_msg);
        textView1.setText(startTime);
        textView2.setText(timelong);
        textView3.setText(disLong);
        textView4.setText("数据不能下发到床");
        final TextView btnYes = (TextView) view.findViewById(R.id.ok_btn_queding);
        final TextView btlNo = (TextView) view.findViewById(R.id.no_btn_quxiao);
        final MyDialog builder = new MyDialog(mActivity,0,0,view,R.style.DialogTheme);

        builder.setCancelable(false);
        builder.show();
        //设置对话框显示的View
        //点击确定是的监听
        btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                showFailDialog("重启失败，请检查网络");
                builder.cancel();
            }
        });


        btlNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFailDialog("保修失败，请检查网络");
                builder.cancel();
            }
        });

    }
    private void showFindDialog(String timeLong){
        View view = mActivity.getLayoutInflater().inflate(R.layout.dialog_find, null);
        TextView textView1 = (TextView)view.findViewById(R.id.tv_strat_time);
        TextView textView2 = (TextView)view.findViewById(R.id.tv_run_time);
        textView1.setText(startTime);
        textView2.setText(timeLong);
        final MyDialog builder = new MyDialog(mActivity,0,0,view,R.style.DialogTheme);
        builder.setCancelable(true);
        builder.show();
    }

    public String getTime(String newTime,String oldTime){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date d1 = df.parse(newTime);
            Date d2 = df.parse(oldTime);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);

            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
            return ""+days+"天"+hours+"小时"+minutes+"分";
        }catch (Exception e)
        {
        }
        return null;
    }
    private void showFailDialog(String message){
        View view = mActivity.getLayoutInflater().inflate(R.layout.add_bed_fail_dialog, null);
        TextView tvContent = (TextView)view.findViewById(R.id.tv_dialog_content);
        tvContent.setText(message);
        final MyDialog builder = new MyDialog(mActivity,0,0,view,R.style.DialogTheme);
        builder.setCancelable(true);
        builder.show();
    }
}
